<?php

namespace app\supplier\controller\product;

use app\supplier\controller\Controller;
use app\supplier\model\product\ProductMark as MarkModel;
use app\supplier\model\supplier\Supplier;

/**
 * 标记管理控制器
 */
class ProductMark extends Controller
{
    /**
     * 获取列表
     */
    public function index()
    {
        // 列表
        $model   = new MarkModel;
        $list    = $model->getList($this->request->param(), $this->getSupplierId());
        // 数量
        $category_count = [];
        $category_count = [
            'using'   => $model->getMarkTotal(['audit_status' => 1], $this->getSupplierId()),
            'audit'   => $model->getMarkTotal(['audit_status' => 0], $this->getSupplierId()),
            'no_pass' => $model->getMarkTotal(['audit_status' => 2], $this->getSupplierId()),
        ];
        return $this->renderSuccess('', compact('list', 'category_count'));
    }

    /**
     * 添加
     */
    public function add()
    {
        $nosh = Supplier::checkauth($this->getSupplierId(),'mark');
        $model = new MarkModel;
        // 新增记录
        if ($model->add(array_merge($this->postData(), ['audit_status' => $nosh?1:0]), $this->getSupplierId())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     */
    public function edit($id)
    {
        $nosh = Supplier::checkauth($this->getSupplierId(),'mark');
        // 详情
        $model = MarkModel::detail($id);
        if ($model->isEmpty() || $model['shop_supplier_id'] != $this->getSupplierId()) {
            return $this->renderError('暂无权限操作');
        }
        // 更新记录
        if ($model->edit(array_merge($this->postData(), ['audit_status' => $nosh?1:0]), $this->getSupplierId())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除
     */
    public function delete($id)
    {
        $model = MarkModel::detail($id);
        if ($model->isEmpty() || $model['shop_supplier_id'] != $this->getSupplierId()) {
            return $this->renderError('暂无权限操作');
        }
        if (!$model->remove()) {
            return $this->renderError('该标签使用中，删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}